package org.child_motivation.integration.dao;

import no.kantega.publishing.security.data.User;
import org.child_motivation.business.model.Group;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class JdbcGroupDao extends JdbcDaoSupport implements GroupDao {
    @Override
    public void saveOrUpdate(Group group) {
        Map<String, Object> params = new HashMap();
        params.put("name", group.getName());
        params.put("owner", group.getOwner().getId());
        params.put("approvers", group.getApproversAsString());
        params.put("members", group.getMembersAsString());
        if(group.isNew()){
            SimpleJdbcInsert insert = new SimpleJdbcInsert(getJdbcTemplate());
            insert.setTableName("pocket_money_group");
            insert.setGeneratedKeyName("id");
            Number key = insert.executeAndReturnKey(params);
            group.setId(key.intValue());
        }else{
            params.put("id", group.getId());
            String sql = "UPDATE pocket_money_group SET name = name, owner = owner, approvers = approvers, members = members WHERE id = id";
            getJdbcTemplate().update(sql,params);
        }
    }

    @Override
    public void deleteGroup(int groupid) {
        String sql = "DELETE FROM pocket_money_group WHERE id = ?";
        getJdbcTemplate().update(sql,groupid);
    }

    @Override
    public List<Group> getGroups(String owner) {
        String sql = "SELECT * FROM pocket_money_group WHERE owner = ?";
        List<Group> groupList= getJdbcTemplate().query(sql, new GroupRowMapper(), owner);
        return groupList;
    }

    @Override
    public Group getGroup(int id) {
        String sql = "SELECT * FROM pocket_money_group WHERE id = ?";
        return getJdbcTemplate().queryForObject(sql, new GroupRowMapper(), id);
    }

    private class GroupRowMapper implements ParameterizedRowMapper<Group> {
        public Group mapRow(ResultSet rs, int i) throws SQLException {
            int id = rs.getInt("id");
            String name = rs.getString("name");
            User owner = new User();
            owner.setId(rs.getString("owner"));
            String approvers = rs.getString("approvers");
            List<User> approverList = createUsersFromString(approvers);
            String members = rs.getString("members");
            List<User> memberList = createUsersFromString(members);
            return new Group(id, name,owner, approverList, memberList);
        }

        private List<User> createUsersFromString(String commaSeparatedString){
            String[] split = commaSeparatedString.split(",");
            List<User> userList = new ArrayList();
            for(String userId: split){
                userId = userId.trim();
                User user = new User();
                user.setId(userId);
                userList.add(user);
            }
            return userList;
        }
    }
}
